#!/usr/bin/env python
# -*- coding:UTF-8 -*-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#            set_python_env_file.py
#
# 文件名称 : set_python_env_file.py
# 文件版本 : V0.01
# 文件描述 : 设置python运行环境的配置文件
# 编写人员 : HHL
# 修改人员 :
# 编修描述 : V0.01，时间2017.03.29
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

import os
import sys
import os.path
import shutil
import traceback

#遍历指定的目录,返回有指定类型数据的目录列表
def walk_py_file_dir(py_dir, _type):
    include_py_dir = []
    for top_dir, c_dir, c_file in os.walk(py_dir):
        #检查目录下是否有指定类型的文件
        if c_file:
            for fi in c_file:
                file_type = fi.split(".")
                #如果有*.py文件，将目录存储到列表
                if _type == file_type[-1]:
                    include_py_dir.append(top_dir)
                    break

    include_py_dir = include_py_dir[1:]
    return(include_py_dir)

#复制配置文件到目录
def cp_py_version_file(py_file, py_dir):
    s = 0
    d_dir = walk_py_file_dir(py_dir, "py")
    if os.path.exists(py_file):
        for d in d_dir:
            s += 1
            shutil.copy(py_file, d)
        print("success %d times copy file" %(s))
    else:
        print("the file of setting python version ")

#删除配置文件到目录
def rm_py_version_file(py_file, py_dir):
    s = 0
    tmp_path = ""
    py_file = os.path.basename(py_file)
    d_dir = walk_py_file_dir(py_dir, "py")
    for d in d_dir:
        tmp_path = os.path.join(d, py_file)
        if os.path.exists(tmp_path):
            os.remove(tmp_path)
            s += 1
        else:
            print("no file in the path: %s" %(d))

    print("success remove %d file" %(s))

#参数说明：
#参数1：删除(rm)或复制(cp)
#参数2：传输的文件名称，默认是 .python-version
#参数3：操作目录，默认是当前目录下
def set_python_env_file():

    py_dir   = ""
    py_file  = ""
    py_order = ""

    #检查有没有参数
    if 1 == len(sys.argv):
        print("please input the argument of necessary")
        return

    #读取和判断具体的参数
    for argv in sys.argv[1:]:
        if "--help" == argv:
            print("infomation help")
            return
        elif "-rm" == argv or "-cp" == argv:
            py_order = argv
        else:
            argv = os.path.abspath(argv)
            if os.path.isdir(argv):
                py_dir = (argv)
            else:
                if os.path.isfile(argv):
                    if os.path.exists(argv):
                        py_file = argv

    #如果参数中的路径为空，获取q当前路径
    if "" == py_dir:
        py_dir = os.getcwd()

    #如果参数中的路径配置文件为空，默认文件名是 .python-version
    if "" == py_file:
        py_file = os.path.abspath(".python-version")
        if False == os.path.exists(py_file):
            print("no such file \".python-version\", no python env setting file")
            return

    print("the operation order = %s" %(py_order))
    print("the operation dir   = %s" %(py_dir  ))
    print("the operation file  = %s" %(py_file ))

    if "-rm" == py_order:
        rm_py_version_file(py_file, py_dir)
    elif "-cp" == py_order:
        cp_py_version_file(py_file, py_dir)
    else:
        print("the operation order is error, it is must /"-cp/" or /"-rm/"")
        return


#程序入口
if __name__ == '__main__':
    set_python_env_file()
